package BackTrackingSeries;

import java.util.ArrayList;
import java.util.List;

public class PartitionSolution_131 {
    List<List<String>> result = new ArrayList<>();
    List<String> path = new ArrayList<>();
    StringBuilder sb = new StringBuilder();

    public List<List<String>> partition(String s) {
        backTracking(s, 0, sb);
        return result;
    }

    // 组合问题
    public void backTracking(String s, int index, StringBuilder sb) {

        if (index == s.length()) {
            result.add(new ArrayList<>(path));
            return;
        }

        for (int i = index; i < s.length(); i++) {
            sb.append(s.charAt(i));
            if (isHuiWen(sb)) {
                path.add(sb.toString());
                backTracking(s, i + 1, new StringBuilder());
                path.remove(path.size() - 1);
            }
        }
    }

    // 判断字符串是不是回文串
    public Boolean isHuiWen(StringBuilder sb) {
        int left = 0;
        int right = sb.length() - 1;
        while (left <= right) {
            if (sb.charAt(left) == sb.charAt(right)) {
                left++;
                right--;
            } else {
                return false;
            }
        }
        return true;
    }
}
